// simple fragment shader

uniform sampler2D sampler;

uniform bool selected;
uniform float sRepeat;
uniform float tRepeat;
uniform float centerX;
uniform float centerY;
uniform float centerZ;

varying vec3 vertex;

const float pi = 3.1415;

vec2 getST()
{
    vec3 center = vec3(centerX, centerY, centerZ);
    vec3 r = normalize(vertex - center);
    float alpha = asin(r.y);
    float beta = atan(r.x, r.z);
    return vec2((beta/(2.0*pi)) * sRepeat, (alpha/pi+0.5) * tRepeat);
}

void main()
{
	vec4 color = texture2D(sampler, getST());
	if (!selected) color -= 0.3;
	gl_FragColor = color;
}